<template>
  <div class="wrapper">
    <header-view></header-view>
    <div class="banner">
      <img src="~img/bg2.jpg" alt="" />
    </div>
    <div class="main" v-if="info">
      <div class="breadcrumb-view" v-if="loadFinish && info.cate_name != null">
        <div class="label">{{ info.cate_name.one.cate_name }}<span class="icon2"></span></div>
        <div class="right">
          当前位置：
          <el-breadcrumb separator-class="el-icon-d-arrow-right">
            <el-breadcrumb-item>首页</el-breadcrumb-item>
            <el-breadcrumb-item>{{ info.cate_name.two.cate_name }}</el-breadcrumb-item>
            <el-breadcrumb-item>{{ info.cate_name.one.cate_name }}</el-breadcrumb-item>
            <el-breadcrumb-item>正文</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
      </div>
      <div class="detail" v-if="loadFinish">
        <div class="title">{{ info.title }}</div>
        <div class="video-view" v-if="info.mp4_url">
          <video :src="info.mp4_url" controls @play="onVideoPlay"></video>
        </div>
        <div class="tab-view" v-if="info.is_mp4 == 1">
          <div class="tab">
            <div :class="{ active: tabIndex === 0 }" @click="tabIndex = 0">课程介绍</div>
            <div :class="{ active: tabIndex === 1 }" @click="tabIndex = 1">授课教师</div>
            <div :class="{ active: tabIndex === 2 }" @click="tabIndex = 2">课程评价</div>
          </div>
          <div class="num">
            <img src="~img/video2.png" /> <b>{{ playNum }}</b> <span>次播放</span>
          </div>
        </div>
        <div class="content" v-html="info.contents" v-if="tabIndex === 0"></div>
        <div class="teacher-view" v-if="tabIndex === 1">
          <div class="label"><span></span>教师团队</div>
          <div class="list">
            <div class="li" v-for="(item, index) of teacherList" :key="index">
              <div class="img">
                <img :src="item.image" alt="" />
              </div>
              <div class="name">{{ item.name }}</div>
              <div class="li-title">{{ item.posts }}</div>
              <div class="school">
                <div>{{ item.school }}</div>
              </div>
              <div class="desc">{{ item.introduce }}</div>
            </div>
          </div>
        </div>
        <div class="comment-view" v-if="tabIndex === 2">
          <div class="score-view">
            <div class="num">{{ score }}</div>
            <div class="score">
              <div class="bg2"></div>
              <div class="bg1" :style="'width: ' + score * 20 + '%'"></div>
            </div>
            <div class="total">共{{ commentTotal }}条评价</div>
            <div class="btn" @click="openComment"><img src="~img/edit.png" alt="" />我要评价</div>
          </div>
          <div class="list">
            <div class="li" v-for="(item, index) of commentList" :key="index">
              <div class="img">
                <img src="~img/avatar.png" alt="" />
              </div>
              <div class="con">
                <div class="name-view">
                  <div class="name">{{ item.nickname }}</div>
                  <div class="score">
                    <div class="bg2"></div>
                    <div class="bg1" :style="'width: ' + item.evaluation_level * 20 + '%'"></div>
                  </div>
                </div>
                <div class="desc" v-html="item.content"></div>
                <div class="time"><img src="~img/time.png" /> {{ item.add_time }}</div>
              </div>
            </div>
          </div>

          <div class="pagination-view" v-if="this.listTotal">
            <el-pagination
              background
              layout="prev, pager, next"
              :total="listTotal"
              :page-size="perpage"
              @current-change="handleCurrentChange"
            >
            </el-pagination>
          </div>
        </div>
        <div class="other">
          <div v-if="prev" @click="openDetail(prev.article_id)">上一篇：{{ prev.title }}</div>
          <div v-if="next" @click="openDetail(next.article_id)">下一篇：{{ next.title }}</div>
        </div>
      </div>
    </div>
    <comment-view ref="commentDom" @reload="getCommentReload"></comment-view>
    <bottom-view></bottom-view>
  </div>
</template>

<script>
import { articleDetailApi, articlePlayApi, articlePlayNumApi, commentsListApi } from '@/api/api'
import headerView from '@/components/headerView'
import bottomView from '@/components/bottomView'
import commentView from '@/components/commentView'
export default {
  name: 'articleDetail',
  components: {
    headerView,
    bottomView,
    commentView
  },
  data() {
    return {
      id: this.$route.query.id,
      info: null,
      playNum: 0,
      prev: null,
      next: null,
      playOnce: false,
      tabIndex: 0,
      teacherList: [],
      page: 1,
      perpage: 10,
      listTotal: 0,
      commentList: [],
      commentTotal: 0,
      score: 0,
      loadFinish: false
    }
  },
  mounted() {
    this.getInfo()
    this.getNum()
  },
  methods: {
    async getInfo() {
      const { errno, data } = await articleDetailApi({
        article_id: this.$route.query.id
      })
      if (errno === 0) {
        this.info = data.info
        this.teacherList = data.instructors.map(item => {
          item.image = item.image || require('@/assets/avatar.png')
          return item
        })
        if (Array.isArray(this.info.cate_name)) {
          this.info.cate_name = null
        }
        if (this.info.is_mp4 == 1) {
          this.getComments()
        }
        this.prev = data.top
        this.next = data.next
        this.loadFinish = true
      }
    },
    async getNum() {
      const { errno, data } = await articlePlayNumApi({
        article_id: this.$route.query.id
      })
      if (errno === 0) {
        this.playNum = data.num
      }
    },
    getCommentReload() {
      this.page()
      this.getComments()
    },
    async getComments() {
      const { errno, data } = await commentsListApi({
        article_id: this.$route.query.id,
        page: this.page,
        perpage: this.perpage
      })
      if (errno === 0) {
        let score = parseFloat(data.comments.evaluation_num) / data.comments.comments_num
        this.score = parseFloat(
          score
            .toFixed(2)
            .slice(0, -1)
            .toString()
        )
        this.commentTotal = data.comments.comments_num
        this.commentList = data.list.data.map(item => {
          item.add_time = this.COMMON.handleTime(item.add_time)
          return item
        })
        this.listTotal = data.list.total
      }
    },
    handleCurrentChange(page) {
      this.page = page
      this.getComments()
    },
    onVideoPlay() {
      console.log('视频开始播放')
      if (!this.playOnce) {
        articlePlayApi({
          article_id: this.$route.query.id
        }).then(res => {
          if (res.errno == 0) this.playNum++
        })
      }
      this.playOnce = true
    },
    openDetail(id) {
      this.$router.goto({
        path: '/articleDetail',
        query: {
          id: id
        }
      })
    },
    openComment() {
      if (this.TOKEN) {
        this.$refs.commentDom.open(this.id)
      } else {
        this.eventBus.$emit('openLogin')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.banner {
  height: 640px;
  width: 100%;
  img {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
}
.main {
  width: 1264px;
  min-height: 600px;
  margin: 0 auto;
  padding-top: 100px;
  padding-bottom: 80px;
  .breadcrumb-view {
    height: 32px;
    line-height: 32px;
    overflow: hidden;
    font-size: 24px;
    .label {
      float: left;
      .icon2 {
        transform: scale(0.5);
        margin-bottom: 2px;
      }
    }
    .right {
      float: right;
      /deep/ .el-breadcrumb {
        font-size: 24px;
        display: inline-block;
        vertical-align: top;
        margin-top: 3px;
      }
      /deep/ .el-breadcrumb__inner,
      /deep/ .el-breadcrumb__separator {
        color: #4d4d4d;
      }
      /deep/ .el-breadcrumb__separator {
        font-size: 20px;
      }
    }
  }
  .detail {
    margin-top: 66px;
    background: #ffffff;
    padding: 52px 50px 85px 50px;
    .title {
      line-height: 70px;
      font-size: 44px;
      text-align: center;
      margin-bottom: 80px;
    }
    .video-view {
      width: 1164px;
      height: 573px;
      margin-bottom: 80px;
      video {
        width: 100%;
        height: 100%;
        background: #000;
        border-radius: 10px;
      }
    }
    .tab-view {
      height: 54px;
      margin-bottom: 99px;
      .tab {
        height: 54px;
        line-height: 54px;
        float: left;
        position: relative;
        color: #333;
        font-size: 28px;
        background: #f2f5f8;
        border-radius: 35px 35px 35px 35px;
        div {
          float: left;
          width: 200px;
          border-radius: 27px 27px 27px 27px;
          text-align: center;
          cursor: pointer;
          &.active {
            color: #fff;
            background: @base-color;
          }
        }
      }
      .num {
        float: right;
        height: 26px;
        line-height: 26px;
        font-size: 27px;
        margin-top: 14px;
        img {
          width: 26px;
          height: 26px;
          vertical-align: top;
        }
        b {
          color: @base-color;
          font-weight: 700;
          vertical-align: top;
          padding: 0 10px 0 10px;
        }
        span {
          vertical-align: top;
        }
      }
    }
    .content {
      width: 1164px;
      margin: 0 auto;
      /deep/ img {
        max-width: 1164px;
      }
    }
    .teacher-view {
      .label {
        height: 38px;
        line-height: 38px;
        font-weight: 600;
        font-size: 26px;
        color: #000000;
        margin-bottom: 37px;
        span {
          width: 5px;
          height: 30px;
          vertical-align: top;
          margin-top: 4px;
          margin-right: 9px;
          background: @base-color;
          border-radius: 3px;
          display: inline-block;
        }
      }
      .list {
        overflow: hidden;
        .li {
          float: left;
          width: 262px;
          height: 310px;
          background: #f2f5f8;
          border-radius: 8px;
          text-align: center;
          padding-top: 27px;
          margin-right: 18px;
          margin-bottom: 20px;
          &:nth-child(4n) {
            margin-right: 0;
          }
          .img {
            height: 60px;
            margin-bottom: 7px;
            text-align: center;
            img {
              width: 60px;
              height: 60px;
              border-radius: 50%;
            }
          }
          .name {
            font-size: 16px;
            color: @base-color;
            line-height: 30px;
          }
          .li-title {
            font-size: 16px;
            color: #333;
            line-height: 30px;
          }
          .school {
            font-size: 14px;
            color: @base-color;
            div {
              display: inline-block;

              height: 24px;
              line-height: 24px;
              background: #edab81;
              border-radius: 12px;
              padding: 0 13px;
            }
          }
          .desc {
            height: 100px;
            padding: 0 16px;
            margin-top: 20px;
            color: #525252;
            text-align: left;
            font-size: 14px;
            line-height: 20px;
            overflow-y: auto;
          }
        }
      }
    }
    .comment-view {
      .score-view {
        overflow: hidden;
        height: 62px;
        .num {
          float: left;
          height: 62px;
          line-height: 62px;
          font-weight: 600;
          font-size: 43px;
          color: #f3a738;
        }
        .total {
          font-size: 22px;
          float: left;
          height: 62px;
          line-height: 62px;
        }
        .score {
          width: 243px;
          height: 34px;
          float: left;
          position: relative;
          margin-left: 16px;
          margin-right: 35px;
          margin-top: 14px;
          .bg1 {
            width: 243px;
            height: 34px;
            background: url(../assets/star-1.png) no-repeat;
            background-size: auto 34px;
            position: absolute;
            left: 0;
            top: 0;
          }
          .bg2 {
            width: 243px;
            height: 34px;
            background: url(../assets/star-2.png) no-repeat;
            background-size: 100% 100%;
          }
        }
        .btn {
          margin-top: 8px;
          float: right;
          width: 156px;
          height: 44px;
          line-height: 42px;
          text-align: center;
          border-radius: 27px;
          border: 1px solid @base-color;
          color: @base-color;
          font-size: 24px;
          cursor: pointer;
          img {
            width: 20px;
            height: 20px;
            margin-right: 5px;
            vertical-align: top;
            margin-top: 12px;
          }
        }
      }
      .list {
        .li {
          display: flex;
          border-bottom: 1px solid #d6d6d6;
          padding-bottom: 48px;
          padding-top: 48px;
          .img {
            height: 70px;
            height: 70px;
            img {
              width: 70px;
              height: 70px;
              border-radius: 50%;
            }
          }
          .con {
            padding-left: 22px;
            flex: 1;
            .name-view {
              overflow: hidden;
              line-height: 28px;
              margin-bottom: 13px;
              .name {
                float: left;
                font-size: 20px;
                color: #000000;
              }
              .score {
                width: 150px;
                height: 21px;
                float: left;
                position: relative;
                margin-left: 22px;
                margin-top: 3px;
                .bg1 {
                  width: 150px;
                  height: 21px;
                  background: url(../assets/star-1.png) no-repeat;
                  background-size: auto 21px;
                  position: absolute;
                  left: 0;
                  top: 0;
                }
                .bg2 {
                  width: 150px;
                  height: 21px;
                  background: url(../assets/star-2.png) no-repeat;
                  background-size: 100% 100%;
                }
              }
            }
            .desc {
              font-size: 20px;
              color: #525252;
              line-height: 28px;
              margin-bottom: 18px;
            }
            .time {
              font-size: 18px;
              color: #9a9a9a;
              line-height: 28px;
              img {
                width: 18px;
                height: 18px;
                vertical-align: top;
                margin: 5px 5px 0 0;
              }
            }
          }
          &:last-child {
            border-bottom: 0;
          }
        }
      }
    }
    .other {
      margin-top: 153px;
      font-size: 20px;
      color: @base-color;
      line-height: 44px;
      div {
        cursor: pointer;
      }
    }
  }
}

.pagination-view {
  text-align: center;
  padding: 23px 0 23px 0;
}
</style>
